package com.lh.java.connect;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.io.IOUtils;

import cn.winclick.ad.client.domain.DBbean;
import cn.winclick.ad.client.log.Logger;
import cn.winclick.ad.client.log.LoggerImpl;
import cn.winclick.ad.client.utils.Constants;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/*****************************
 * @Title: DBConnection.java
 * @Package cn.winclick.ad.client.utils
 * @author 黄升
 * @Description:数据库多源切换连接
 * @date 2013-3-20 上午12:13:02
 * @version V1.0
 ****************************** 
 */
public class TestC3p0NotShareConnection  {

	private static Logger logger = new LoggerImpl(TestC3p0NotShareConnection.class);

	private static ComboPooledDataSource mydataSource;

	/*** 静态文件加载 ****/
	static {
		System.out.println("初始化开始....");
		mydataSource = new ComboPooledDataSource(
				"DD_SERVER");

		System.out.println("初始化结束");
	}

	private TestC3p0NotShareConnection() {
	}

	

	public Connection getConnection() {
		try {
			return mydataSource.getConnection();
		} catch (SQLException e) {
			logger.debug("get connetcion failed "+e);
			e.printStackTrace();
			return null;
		}

	}

	


	public static void main(String[] args) {
		Set<Connection> conSet = new HashSet<Connection>();
		TestC3p0NotShareConnection c= new TestC3p0NotShareConnection();
		for(int i=0;i<50;i++){
			Connection con =c.getConnection();
			if(conSet.contains(con)){
				System.out.println(con+"  has aready exsixts");
			}else {
				conSet.add(con);
				try {
					con.close();
				} catch (SQLException e) {
					System.out.println("con close excption");
					e.printStackTrace();
				}
			}		
			
		}
		int j=1;
		for(Connection con:conSet){
			System.out.println(j+"   "+con);
			j++;
		}
		
		System.out.println(conSet.size());
		// for(int i=0;i<50;i++) {
		// DBConnection.getDBConnectionInstance().getConnection();
		// }
		// for(int j=0;j<1000;j++) {
		// PersonService personService = new PersonServiceImpl();
		// List listperson = personService.findPerson("");
		// System.out.println(listperson.size());
		// }
	}
}
